package com.me.info;

import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.me.constant.Constant;
import com.me.control.CreateWindown;
import com.me.enums.ScreenEnum;
import com.me.helper.PreferencesData;
import com.me.memorytest.MainMemoryTest;
import com.me.screen.BaseScreen;
import com.me.ui.HeaderGame;
import com.me.utils.ProcessData;

public class MyImage {

	public int iValueSelected;
	private TextureRegionDrawable changeImage;
	private TextureRegionDrawable defaultImage;
	private Image image;
	private int posInList;
	private Label namePic = new Label("", Constant.skin, "labelNameCategory");

	public Image getImg() {
		return image;
	}

	public void setImg(Image img) {
		this.image = img;
	}

	public MyImage(TextureRegionDrawable defaultImage,
			TextureRegionDrawable changedImage, int iValue, int posInlist) {

		BaseScreen.stage.clear();

		this.iValueSelected = iValue;
		this.posInList = posInlist;
		this.defaultImage = defaultImage;
		this.changeImage = changedImage;
		namePic.setX(Constant.STAGE_WIDTH / 2 - 20);
		namePic.setY(Constant.STAGE_HEIGHT);
		image = new Image();
		image.setDrawable(new TextureRegionDrawable(this.defaultImage));
		image.addListener(new ClickListener() {

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				PlayBoardInfo.iClick++;
				if (PlayBoardInfo.iClick >= 3) {
					return;
				}

				if (PreferencesData.getBoolRememberSound())
					Constant.clickSound.play();

				Constant.iCountClicked++;
				HeaderGame.lb_Click.setText("Clicked: "
						+ Constant.iCountClicked);
				if (PlayBoardInfo.iClick == 1) {
					PlayBoardInfo.firstPos = posInList;
					changeImage();
				} else if (PlayBoardInfo.iClick == 2) {
					PlayBoardInfo.secondPos = posInList;

					changeImage();

					if (PlayBoardInfo.check()) {
						if (PreferencesData.getBoolRememberSound())
							Constant.matchMusic.play();

						namePic.setText(ProcessData.getName(PlayBoardInfo.listImage
								.get(PlayBoardInfo.secondPos).iValueSelected));
						namePic.addAction(Actions.sequence(Actions.moveTo(
								Constant.STAGE_WIDTH / 2 - 20,
								Constant.STAGE_HEIGHT / 2, 0.8f), Actions
								.scaleTo(1.5f, 1.5f, 0.3f), Actions
								.removeActor()));
						BaseScreen.stage.addActor(namePic);

						Task task = new Task() {
							@Override
							public void run() {

								PlayBoardInfo.listImage
										.get(PlayBoardInfo.firstPos).getImg()
										.remove();
								PlayBoardInfo.listImage
										.get(PlayBoardInfo.secondPos).getImg()
										.remove();
								for (int i = 0; i < PlayBoardInfo.listImage
										.size(); i++)
									PlayBoardInfo.listImage.get(i).getImg()
											.setTouchable(Touchable.enabled);
								PlayBoardInfo.iCountEqual++;
								PlayBoardInfo.iClick = 0;
								if (PlayBoardInfo.iCountEqual == PlayBoardInfo.listImage
										.size() / 2) {
									Constant.LEVEL++;
									if (Constant.LEVEL > Constant.MAX_LEVEL) {
										if (!Constant.b_custom) {
											PreferencesData.setLevel(1);
											MainMemoryTest
													.getScreenManager()
													.createScreen(
															ScreenEnum.END_GAME);
											MainMemoryTest.getScreenManager()
													.getScreen().show();
										} else {
											MainMemoryTest
													.getScreenManager()
													.createScreen(
															ScreenEnum.MENU_SCREEN);
											MainMemoryTest.getScreenManager()
													.getScreen().show();
										}
									} else {
										BaseScreen.stage
												.addActor(HeaderGame.img_bgHide);
										BaseScreen.stage.addActor(CreateWindown
												.createWindown_showResult());
									}
								}
							}
						};
						Timer timer = new Timer();
						timer.scheduleTask(task, 1f);

					} else {
						Task task = new Task() {
							@Override
							public void run() {
								PlayBoardInfo.reset();
								PlayBoardInfo.iClick = 0;
							}
						};
						Timer timer = new Timer();
						timer.scheduleTask(task, 1f);
					}
				}
			}

		});
	}

	public void changeImage() {

		this.getImg().setOrigin(getImg().getWidth() / 2,
				getImg().getHeight() / 2);
		this.getImg().addAction(Actions.scaleTo(0, 0, 0.1f));
		Task task1 = new Task() {
			@Override
			public void run() {
				getImg().setDrawable(new TextureRegionDrawable(changeImage));
				getImg().addAction(Actions.scaleTo(1, 1, 0.05f));
			}
		};
		Timer timer1 = new Timer();
		timer1.scheduleTask(task1, 0.1f);
	}

	public void reset() {
		this.getImg().setDrawable(new TextureRegionDrawable(defaultImage));
		this.getImg().addAction(
				Actions.sequence(Actions.scaleTo(0, 0),
						Actions.scaleTo(1, 1, 0.1f)));
	}
}
